package android.songfinder;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.BaseAdapter;
import android.text.Editable;
import android.text.TextWatcher;

public class ListMaintenanceActivity extends Activity {
    public static ArrayList<String> displayItems = new ArrayList<String>();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	setContentView(R.layout.listmaintenance);
    	
    	//@+id/lblSongFile
    	final TextView lv1=(TextView)findViewById(R.id.lblSongFile);
    	lv1.setText(SongFinderListViewAdapter.CurrentSelItem);
    	
    	final ListView lv = (ListView)findViewById(R.id.ListNames);
    	lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , displayItems));
    	
    	final Button newListBtn = (Button)findViewById(R.id.btnNewList);
    	final EditText newListName = (EditText)findViewById(R.id.txtNewListName);
    	final String newListNameValue = null;
    	
    	List<Integer> sondistIds = new SongListDatabase(ListMaintenanceActivity.this).
    			GetListIdsForSong(SongFinderListViewAdapter.CurrentSelItem);
    	
    	List<ListEntry> list = ListEntry.GetAll(this);
    	for (int i = 0; i < list.size(); i++) {
    		if (!sondistIds.contains(list.get(i).getId())) {
    			displayItems.add(list.get(i).getListName());
    		}
		}
    	
    	newListBtn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String sName=(String) lv1.getText();
				String newListNameValue = newListName.getText().toString();
				if (newListNameValue.length() == 0) {
					showAlert("Must enter a list name");
				}
				if (new SongListDatabase(ListMaintenanceActivity.this).checkListTitleExists(newListNameValue)) {
					showAlert("List already exists-" + newListName.getText().toString());
				} else {
					//displayItems.add(newListName.getText().toString());
					//((BaseAdapter) lv.getAdapter()).notifyDataSetChanged();
					//lv.getI
					new SongListDatabase(ListMaintenanceActivity.this).
						createListAndAddSong(newListNameValue, lv1.getText().toString());
					Intent myIntent = new Intent(ListMaintenanceActivity.this, SongFinderActivity.class);
					startActivity(myIntent);
				}
			}
		});
    }
    
    private void showAlert(String msg) {
		new AlertDialog.Builder(ListMaintenanceActivity.this)

		.setTitle("SongFinder Alert!")

		.setMessage(msg)
		.setNeutralButton("Ok",

		new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog,

		int which) {

		}

		}).show();
    }
}
